using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace MemoryGame
{
    public class Dialog
    {
        public const int DIALOG_TYPE_CONFIRM = 0;
        public const int DIALOG_TYPE_NOTICE = 1;
        //public static int DIALOG_TYPE_CONFIRM = -1;

        //
        public const int DIALOG_NEXTSTATE_EXIT = 0;
        public const int DIALOG_BACK_TO_MAINMENU_EXIT = 1;
        public const int DIALOG_RESTART_GAME = 2;
        //
        public static int dialogType = -1;
        public static int dialogNextState = -1;
        public static string dialogText = null;

	//
	public static int DIALOG_CONFIRM_X = DEF.SCREEN_WIDTH / 20;
    public static int DIALOG_CONFIRM_Y = DEF.SCREEN_HEIGHT / 4;
    public static int DIALOG_CONFIRM_W = DEF.SCREEN_WIDTH - 2 * DEF.SCREEN_WIDTH / 20;
    public static int DIALOG_CONFIRM_H = DEF.SCREEN_HEIGHT - 2 * DEF.SCREEN_HEIGHT / 4;

        public static Rectangle rectF;
        public static bool isShowDialog = false;

        public static void showDialog(int type, string textStringLabel, string textString, int nextState)
        {
            isShowDialog = true;
            dialogType = type;
            dialogText = textString;
            dialogNextState = nextState;
            rectF = new Rectangle(DEF.SCREEN_WIDTH / 6, DEF.SCREEN_HEIGHT / 6, DEF.SCREEN_WIDTH / 6 + DIALOG_CONFIRM_W, DEF.SCREEN_HEIGHT / 6 + DIALOG_CONFIRM_H);
        }

        public static void hideDialog()
        {
            isShowDialog = false;
        }

        public static void drawDialog(SpriteBatch c)
        {

            var t = new Texture2D(Game1.graphicsDevice, 1, 1);
            t.SetData(new[] { Color.White });
            c.Draw(t, new Rectangle(DIALOG_CONFIRM_X, DIALOG_CONFIRM_Y, DIALOG_CONFIRM_W, DIALOG_CONFIRM_H), new Color(0, 0, 0, 220));

            Vector2 tempMeasure = Game1.fontNormal.MeasureString(dialogText);
           
            // ChristmasActivity.fontbig_White.drawString(c, dialogText, DEF.SCREEN_WIDTH / 2, DIALOG_CONFIRM_Y + offset, DIALOG_CONFIRM_W - 100, BitmapFont.ALIGN_CENTER);

            //draw text
		int y = DIALOG_CONFIRM_Y + DIALOG_CONFIRM_H - 3*DEF.DIALOG_BUTTON_CONFIRM_W/2;// PikachuActivity.SCREEN_HEIGHT / 6 + DIALOG_CONFIRM_H - 40 - 20;
            int x1 = DIALOG_CONFIRM_X + DIALOG_CONFIRM_W / 4;
            int x2 = DIALOG_CONFIRM_X + 3 * DIALOG_CONFIRM_W / 4 - DEF.DIALOG_BUTTON_CONFIRM_W;
            //draw button
            switch (dialogType)
            {
                case DIALOG_TYPE_CONFIRM:
                    c.DrawString(Game1.fontNormal, dialogText, new Vector2((DEF.SCREEN_WIDTH - (int)tempMeasure.X) / 2, DEF.SCREEN_HEIGHT / 2 - (int)tempMeasure.Y), Color.White);
                    if (InputState.isTouchDrapInRect(x1, y, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))
                        Game1.spriteBatch.Draw(DEF.texttureButtonMenu[DEF.FRAME_OK_HIGHTLIGHT], new Vector2(x1, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                    else
                        Game1.spriteBatch.Draw(DEF.texttureButtonMenu[DEF.FRAME_OK_NORMAL], new Vector2(x1, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);


                    if (InputState.isTouchDrapInRect(x2, y, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))
                        Game1.spriteBatch.Draw(DEF.texttureButtonMenu[DEF.FRAME_CANCEL_HIGHTLIGHT], new Vector2(x2, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                    else
                        Game1.spriteBatch.Draw(DEF.texttureButtonMenu[DEF.FRAME_CANCEL_NORMAL], new Vector2(x2, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);

                    break;
                case DIALOG_TYPE_NOTICE:
                    c.DrawString(Game1.fontNormal, dialogText, new Vector2((DEF.SCREEN_WIDTH - (int)tempMeasure.X) / 2, DEF.SCREEN_HEIGHT / 2 - (int)tempMeasure.Y), Color.Green);
                  //  if (InputState.isTouchDrapInRect(x2, y, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))
                  //      Game1.spriteBatch.Draw(DEF.texttureButtonMenu[DEF.FRAME_OK_HIGHTLIGHT], new Vector2(x2, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                  //  else
                   //     Game1.spriteBatch.Draw(DEF.texttureButtonMenu[DEF.FRAME_OK_NORMAL], new Vector2(x2, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                    break;
                default:
                    break;
            }
            //		fontsmall_White.drawString(mainCanvas, arrayMenuString[i], MENU_POSITION_X, y - 20, BitmapFont.ALIGN_CENTER);		

        }

        public static void updateDialog()
        {
		int y =DIALOG_CONFIRM_Y + DIALOG_CONFIRM_H - 3*DEF.DIALOG_BUTTON_CONFIRM_H/2;//
            int x1 = DIALOG_CONFIRM_X + DIALOG_CONFIRM_W / 4;
            int x2 = DIALOG_CONFIRM_X + 3 * DIALOG_CONFIRM_W / 4 - DEF.DIALOG_BUTTON_CONFIRM_W;

            switch (dialogType)
            {
                case DIALOG_TYPE_CONFIRM:

                    if (InputState.isTouchReleaseInRect(x1, y, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))
                    {
                        SoundManager.playSound(SoundManager.SOUND_SELECT);
                        Dialog.hideDialog();
                        switch (dialogNextState)
                        {
                            case DIALOG_NEXTSTATE_EXIT:
                                Game1.instanceGame1.Exit();
                                //  Game1.instanceGame1.ExitGame();
                                break;
                            case DIALOG_BACK_TO_MAINMENU_EXIT:
                                Game1.changeState(Game1.STATE_MAINMENU, true, true);
                                break;
                            case DIALOG_RESTART_GAME:
                                Game1.changeState(Game1.STATE_GAMEPLAY, true, true);
                                break;
                            default:
                                break;
                        }

                    }
                    else if (InputState.isTouchReleaseInRect(x2, y, DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))//ok
                    {
                        SoundManager.playSound(SoundManager.SOUND_SELECT);
                        hideDialog();
                    }
                    break;
                case DIALOG_TYPE_NOTICE:
                    if(InputState.isTouchPressScreen())
                   // if (InputState.isTouchReleaseInRect(x2, y , DEF.DIALOG_BUTTON_CONFIRM_W, DEF.DIALOG_BUTTON_CONFIRM_H))//ok
                    {
                        switch (dialogNextState)
                        {
                            //case DIALOG_NEXTSTATE_EXIT:
                            //	MainActivity.mainActivity.finish();
                            //	break;
                            //case DIALOG_BACK_TO_MAINMENU_EXIT:
                            //	break;

                            default:
                                break;
                        }
                        hideDialog();
                        Game1.isEnableSound = false;
                        Game1.timeBeginCurrentState = Game1.timeCurrent;
                    }
                    break;
                default:
                    break;
            }


        }
    }
}
